/*
 * tuntap.h
 *
 *  Created on: Mar 8, 2011
 *      Author: zhanglei
 */

#ifndef TUNTAP_H_
#define TUNTAP_H_

typedef struct in_addr IA;
typedef struct sockaddr SA;

/* Pseudo Header is used for calculating checksum of TCP and UDP
 * all of them should be converted to Network bytes order. */
typedef struct Pseudo_Header {
    uint32_t src; /* source ip address of the TCP/UDP packet */
    uint32_t dst; /* destination ip address of the TCP/UDP packet */
    uint8_t zero; /* always be zero */
    uint8_t proto; /* indicates TCP or UDP packet */
    uint16_t len; /* packet length */
} Pseudo_Header;

/* open tun/tap device . You can specify tunX or tapX
 * where X refers to device number you want, or you can
 * simply pass tun or tap, then system will choose one
 * for you and returned by dev */
int tun_open(char *dev);
int tun_activate(char *dev);

int tun_set_ip(char *dev, uint32_t ip);
int tun_set_netmask(char *dev, uint32_t mask);

/* analysis and print packet information
 * should be called in debug mode only. */
int decode_packet(const char *pack);

/* packet checksum function .
 * first of all, it calculates IP checksum
 * and then TCP/UDP checksum depending on
 * protocol type. */
int pack_chksum(char *buf, int size);

/* calculates final checksum */
uint16_t chk_calc(uint32_t cksum);

/* add all uint16_t numbers which buffer pionts to */
uint32_t chk_add(uint16_t *buffer, int size);

int get_ipaddr(char *buf, uint32_t *addr, int where);
int set_ipaddr(char *buf, int size, uint32_t addr, int where);
#endif /* TUNTAP_H_ */
